/**
 * @api post control.
 *
 * User: yunshengzhu
 * Date: 2020/12/3
 * Time: 下午3:13
 */
package control

import (
	"card-platform/config"
	"card-platform/model"
	"encoding/json"
	"errors"
	"pkg/cfg"
	"pkg/utils"
	"time"
)

type Card struct {
}

// swagger:operation POST /cardFeed 卡类 cardFeedReq
// ---
// summary: 送卡 cardFeed.
// description: .
//// responses:
////   "200":
////     "$ref": "#/responses/cardFeedResp"
func (a *Card) CardFeed(userId int64, data []byte, param Param) (out interface{}, code int, err error) {
	req := CardFeedReq{}
	err = json.Unmarshal(data, &req)
	if err != nil {
		cfg.LogErr("err:", err)
		return nil, config.ERR_JSON, err
	}

	resp := CardFeedResp{}
	card, err := model.GetCardNumber(req.CardNumber)
	if err != nil && err != model.ErrRecordNotFound {
		cfg.LogErr("err:", err)
		return nil, config.ERR, err
	}

	if card.CardId <= 0 {
		return nil, config.ERR, errors.New("卡号不存在")
	}

	if card.Status != 0 {
		return nil, config.ERR, errors.New("卡号状态异常,不允许赠送")
	}

	if card.UserId != userId {
		return nil, config.ERR, errors.New("当前用户没有此卡号")
	}

	//更新卡号状态
	card.Status = 2
	card.SendTime = time.Now().Unix()

	err = model.UpdateCard(card)
	if err != nil {
		cfg.LogErr("err:", err)
		return nil, config.ERR, err
	}
	resp.CardNumber = card.CardNumber
	resp.CardPwd = card.CardPwd
	return resp, config.OK, nil
}

// swagger:operation POST /cardCollect 卡类 cardCollectReq
// ---
// summary: 获取当前用户卡信息汇总 cardCollect.
// description: .
//// responses:
////   "200":
////     "$ref": "#/responses/cardCollectResp"
func (a *Card) CardCollect(userId int64, data []byte, param Param) (out interface{}, code int, err error) {
	resp := CardCollectResp{}
	//总量
	resp.CardNum, err = model.GetCardCount(userId, []int64{0, 1, 2, 3})
	resp.CardFeedNum, err = model.GetCardCount(userId, []int64{1, 2, 3})
	return resp, config.OK, nil
}

// swagger:operation POST /cardList 卡类 cardListReq
// ---
// summary: 获取用户卡列表 cardList.
// description: .
//// responses:
////   "200":
////     "$ref": "#/responses/cardListResp"
func (a *Card) CardList(userId int64, data []byte, param Param) (out interface{}, code int, err error) {
	req := CardListReq{}
	err = json.Unmarshal(data, &req)
	if err != nil {
		cfg.LogErr("err:", err)
		return nil, config.ERR_JSON, err
	}
	resp := CardListResp{}
	resp.Count, err = model.GetCardCount(userId, req.Status)
	resp.List, err = model.GetCardList(userId, req.Status, req.Page, req.Size)
	return resp, config.OK, nil
}

//创建卡
func createCard(orderNumber string) error {
	order, err := model.GetOrderNumber(orderNumber)
	if err != nil && err != model.ErrRecordNotFound {
		cfg.LogErr("err:", err)
		return err
	}

	if order.Typ != 1 {
		return errors.New("订单类型异常")
	}

	if order.PayStatus != 1 {
		return errors.New("订单未支付")
	}

	num, err := model.GetCardOrderNumberCount(order.OrderNumber)
	if num >= order.Num {
		return errors.New("卡券已经生成")
	}

	newNum := order.Num - num
	var i int64
	i = 1
	for i <= newNum {
		cardNumber := utils.GetRandomStr(20)
		cardPwd := utils.GetRandomStr(8)
		card, err := model.GetCardNumber(cardNumber)
		if err != nil && err != model.ErrRecordNotFound {
			cfg.LogErr("err:", err)
			continue
		}
		if card.CardId > 0 {
			cfg.LogErr("卡号已经存在")
			continue
		}

		card.CardNumber = cardNumber
		card.OrderNumber = orderNumber
		card.CardPwd = cardPwd
		card.UserId = order.UserId
		card.Status = 0
		card.CreateTime = time.Now().Unix()
		card.OrderNumber = orderNumber

		err = model.CreateCard(card)
		if err != nil {
			cfg.LogErr("err:", err)
			continue
		}
		i++
	}

	//判断订单用户等级
	userObj, err := model.GetUserId(order.UserId)
	if err != nil {
		cfg.LogErr("err:", err)
	}

	if userObj.Grade == 1 {
		//修改用户未白金会员
		model.UpdateUserGrade(userObj.UserId, 2)
	}

	return nil
}
